class OrganizationsController < InheritedResources::Base
  respond_to :json
  load_resource

  def assignment_position_standard
    assignment_position_standard = AssignmentPositionStandard.run(organization_id: params[:id], requre_certificate_organs: params[:requre_certificate_organs])
    render CommandJsonSerializer.as_json assignment_position_standard
  end

  def create
    organization = AddOrganization.run organization_params
    render CommandJsonSerializer.as_json organization
  end

  def update
    organization = UpdateOrganization.run organization_params.merge(organization: @organization)
    render CommandJsonSerializer.as_json organization
  end

  def tree
    render json: Organization.tree
  end

  def position_standard
    respond_with QueryPositionsByCategory.query @organization.category.name
  end

  def get_employee_and_positions
    respond_with @organization.get(:users)
  end

  def require_certificate_organs
    respond_with OrganizationQuery.get_require_certificate_organs @organization
  end

  def show
    respond_with OrganizationJsonSerializer.entry_as_json @organization
  end

  private
  def permitted_params
    params.permit(:organization => [:name, :rank, :category])
  end

  def organization_params

    params.permit(:name, :parent_id, :require_certificate_organs).merge(rank: params[:rank][:name]).merge(category: params[:category][:name]).merge(requre_certificate_organs: (params[:requre_certificate_organs]||[]))
  end

end
